package com.iwords.api.web.action;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import com.iwords.service.SentencesServiceLocal;
import com.iwords.utils.CodeUtils;
import com.iwords.constants.Constants;
import com.iwords.utils.JSONObject;
import com.iwords.utils.StringUtils;
import com.iwords.service.object.ExamSentencesEntity;
import com.iwords.api.web.base.BaseAction;

/**
 * @author       BOLIN.HBC 
 * @time         2011-5-7    
 * @description  词句-SERVLET       
 */
@WebServlet(name = "sentencesAction", urlPatterns = { "/sentences" })
public class SentencesAction extends BaseAction<ExamSentencesEntity> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	private SentencesServiceLocal sentencesService;

	public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {

		String action = req.getParameter("action");
		if (Constants.SENTENCE_HOT_ACTION.equals(action)) {
			doGetHotSentences(req, res);
		} else if (Constants.SENTENCE_ALL_ACTION.equals(action)) {
			doGetAllSentences(req, res);

		}

	}

	public void doGetHotSentences(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {

		org.json.JSONObject result = new org.json.JSONObject();
		if (!StringUtils.isNotBlank(req.getParameter("wordid"))) {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, "wordid is null");
			return;
		}
		Long wordid = Long.parseLong(req.getParameter("wordid"));

		result.put(Constants.DEFAULT_VALUE, getDefaultValue(sentencesService.getHotSentences(wordid)));

		result.put(Constants.IS_SUCCESS, Boolean.TRUE);

		jsonPrint(res, result);

	}

	public void doGetAllSentences(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {
		org.json.JSONObject result = new org.json.JSONObject();
		if (!StringUtils.isNotBlank(req.getParameter("wordid"))) {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, "wordid is null");
			return;
		}
		Long wordid = Long.parseLong(req.getParameter("wordid"));
		result.put(Constants.DEFAULT_VALUE, getDefaultValue(sentencesService.getAllSentences(wordid)));
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		jsonPrint(res, result);

	}

	private JSONObject getDefaultValue(List<ExamSentencesEntity> list) {
		JSONObject defaultValue = new JSONObject();
		defaultValue.putColumn("sentenid", "sentence", "paraphrase", "examid", "itemid", "createtime", "updatetime");

		for (ExamSentencesEntity en : list) {
			defaultValue.putValue(en.getSentenceid(), CodeUtils.encode(en.getSentence()), CodeUtils.encode(en.getParaphrase()), en.getExamid(), en.getItemid(),
					en.getCreatetime().getTime(), en.getUpdatetime().getTime());
		}
		return defaultValue;
	}

	public ExamSentencesEntity initEntity(HttpServletRequest req) {
		return null;
	}

}